﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace Data.UserDb
{

    public static class Media
    {

        private static readonly String ConnectString = ConfigurationManager.ConnectionStrings["media.db.server"].ConnectionString;
        #region 新闻操作

        /// <summary>
        /// 获取新闻头topn条
        /// </summary>
        /// <param name="topn">想要获得的条数</param>
        /// <returns>topn条最新新闻</returns>
        public static IEnumerable<T_chnl_news> GetTopnNews(int topn)
        {
            //Data Source=,1433;Initial Catalog=Media_Chnl;Persist Security Info=True;User ID=sa;Password=
            using (var context = new MediaChnlContext(ConnectString))
            {
                return (from news in context.T_chnl_news orderby news.Createtime descending select news).Take(topn).ToList();
            }
        }

        /// <summary>
        /// 添加新闻
        /// </summary>
        /// <param name="msg">新闻内容</param>
        /// <returns>新增新闻的ID</returns>
        public static Int32 AddNews(String msg)
        {
            using (var context = new MediaChnlContext(ConnectString))
            {
                var news = new T_chnl_news
                               {
                                   Createtime = DateTime.Now,
                                   Msg = msg
                               };
                context.T_chnl_news.InsertOnSubmit(news);
                context.SubmitChanges();
                return news.Createid;
            }
        }

        /// <summary>
        /// 获取所有的新闻
        /// </summary>
        /// <returns>新闻内容</returns>
        public static IEnumerable<T_chnl_news> GetAll()
        {
            using (var context = new MediaChnlContext(ConnectString))
            {
                return (from news in context.T_chnl_news orderby news.Createtime descending select news);
            }
        }

        /// <summary>
        /// 删除新闻
        /// </summary>
        /// <param name="newsId">需要删除的新闻ID</param>
        public static void Delete(Int32 newsId)
        {
            using (var context = new MediaChnlContext(ConnectString))
            {
                context.ExecuteCommand("delete from t_chnl_news where createid={0}", newsId);
            }
        }

        #endregion

        #region 房间操作

        /// <summary>
        /// 获取房间列表，按人气排序
        /// </summary>
        /// <returns>人气列表</returns>
        public static IEnumerable<T_chnl_roominfo> GetRoomInfoByExp(Int32 topn)
        {
            using (var context = new MediaChnlContext(ConnectString))
            {
                return (from roomInfo in context.T_chnl_roominfo orderby roomInfo.Exp descending select roomInfo).Take(topn).ToArray();
            }
        }

        public static T_chnl_roominfo GetRoomById(int roomId)
        {
            using(var context = new MediaChnlContext(ConnectString))
            {
                var tmp = (from room in context.T_chnl_roominfo where room.Roomid == roomId select room).Take(1);
                if(tmp.Count() == 0) return null;
                return tmp.ToArray()[0];
            }
        }

        public static void AddRoom(T_chnl_roominfo room)
        {
            using(var context = new MediaChnlContext(ConnectString))
            {
                context.T_chnl_roominfo.InsertOnSubmit(room);
                context.SubmitChanges();
            }
        }

        #endregion


        #region 经典欣赏部分

        public static IEnumerable<T_chnl_room_ad> GetRoomAd()
        {
            using(var context = new MediaChnlContext(ConnectString))
            {
                return (from ad in context.T_chnl_room_ad orderby ad.Times descending select ad).ToList();
            }
        }

        public static void UpdateRoomAd(Int32 id)
        {
            using(var context = new MediaChnlContext(ConnectString))
            {
                context.ExecuteCommand("update t_chnl_room_ad set times=times+1 where createid={0}", id);
            }
        }

        #endregion

        #region 节目部分
        public static IEnumerable<T_chnl_roomprog_top> GetTopProg(int type, int topn)
        {
            using(var context = new MediaChnlContext(ConnectString))
            {
                return
                    (from prog in context.T_chnl_roomprog_top
                     where prog.Type == type
                     orderby prog.Times descending
                     select prog).Take(topn).ToArray();
            }
        }
        #endregion


        public static IEnumerable<T_chnl_user_top> GetUserOrser(int orderType, int topn)
        {
            using(var context = new MediaChnlContext(ConnectString))
            {
                switch (orderType)
                {
                    case 0:
                        return
                            (from u in context.T_chnl_user_top orderby u.Exp descending select u).Take(topn).ToArray();
                    default:
                        return
                            (from u in context.T_chnl_user_top orderby u.Money descending select u).Take(topn).ToArray();
                }
            }
        }

        public static IEnumerable<View_TheaterProgs> GetProgs(int roomId)
        {
           using(var context = new MediaChnlContext(ConnectString))
           {
               return (from p in context.View_TheaterProgs where p.Roomid == roomId select p).ToArray();
           }
        }
    }
}
